package progress.exa34;

public class CodePointStudy {

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
        //test5();
//        test6();
        test7();
    }

    public static void test1(){
        //🍷Hello 需要7个char字符进行存储，其中🍷占两个
        String test="🍷Hello";
        //得到的是代码单元的数量 7 🍷占两个代码单元(两个char)
        System.out.println(test.length());
        // String.codePointCount用来计算字符串中的真实Unicode的数量，也就是码点数量
        // 所以它的结果应该是6
        System.out.println(test.codePointCount(0,test.length()));
    }

    public static void test2(){
        //🍷Hello 需要7个char字符进行存储，其中🍷占两个
        String test="🍷Hello";
        System.out.println(test.charAt(0));
        System.out.println(test.charAt(2));
    }

    public static void test3(){
        //🍷Hello 需要7个char字符进行存储，其中🍷占两个
        String test="🍷Hello";
        int index = test.offsetByCodePoints(0,0);
        int first = test.charAt(0);
        int second = test.charAt(1);
        System.out.println(first);
        System.out.println(second);
        String res = ""+(char)first+(char)second;
        System.out.println(res);
        int cp = test.codePointAt(index);
        System.out.println(cp);
    }

    public static void test4(){
        String test="Hello";
        for(int i=0;i<test.length();i++){
            System.out.print(test.charAt(i));
        }
    }

    /**
     * 遍历带有码点的字符串
     */
    public static void test5(){
        String test="🍷Hello";
        for(int i=0;i<test.length();) {
            int[] cp = new int[1];
            cp[0] =  test.codePointAt(i);
            if(Character.isSupplementaryCodePoint(cp[0])) {
                i+=2;
                System.out.print(new String(cp,0,1));
            } else {
                i++;
                System.out.print((char)cp[0]);
            }

        }
    }

    public static void test6(){
        String test="🍷Hello";
        int [] codePoints = test.codePoints().toArray();
        for(int i=0;i<codePoints.length;i++){
            if(Character.isSupplementaryCodePoint(codePoints[i])){
                System.out.print(new String(new int[]{codePoints[i]},0,1));
            }else{
                System.out.print((char)codePoints[i]);
            }

        }
    }

    /**
     * 码点数组转为字符串
     */
    public static void test7(){
        String test="🍷Hello";

        int [] codePoints=test.codePoints().toArray();

        for(int i=0;i<codePoints.length;i++){
            System.out.print((char)codePoints[i]);
        }
        System.out.println();
        //将码点数组转换成字符串
        String str=new String(codePoints,0,codePoints.length);
        System.out.println(str);
    }


}
